package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.pojo.WmsColumnName;
import com.jy.wms.service.WmsColumnNameService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.List;
import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsColumnNameController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Jane
 * @Date Tue Dec 26 14:00:42 CST 2017
 */
@Controller
@RequestMapping("/wmscolumnname")
@Api(value = "/wmscolumnname", description = "自定义列名")
public class  WmsColumnNameController extends BaseController<WmsColumnNameService,WmsColumnName> {

    @PostMapping(value = "list")
    @ApiOperation(value = "查询自定义列名信息",response = JSONObject.class, notes = "查询自定义列名信息")
    public @ResponseBody JSONArray getWmsColumnName(HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//获取仓库Id
        List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
        String sshzid=hz.get(0).getHzid();//获取货主Id
        List<WmsColumnName> list= baseService.baseSelectPage(sshzid,ckid);
        return JSONArray.fromObject(list);
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除信息",response = JSONObject.class, notes = "删除信息")
    public @ResponseBody JsonResult deleteWmsColumnName(@RequestBody List<String> ids) {
        Integer flag=baseService.baseDeleteBathPrimaryKeys(ids);
        return renderSuccess("删除成功");
    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增自定义列名信息",response = JSONObject.class, notes = "新增自定义列名信息")
    public @ResponseBody JSONObject addWmsColumnName(@RequestBody List<WmsColumnName> list,HttpSession session) {

        try{
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//获取仓库Id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid=hz.get(0).getHzid();//获取货主Id
            baseService.baseInsert(list,sshzid,ckid);//新增自定义列名
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增自定义列名成功","", JSONArray.fromObject(list).toString());
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "新增自定义列名失败", e.getMessage(), "");
        }

    }

    @GetMapping(value = "getWmsColumnName/{id}")
    @ApiOperation(value = "查看单个自定义列名信息",response = JSONObject.class, notes = "GET查看单个自定义列名信息")
    public @ResponseBody WmsColumnName getWmsColumnNameById(@PathVariable("id") String id) {
        WmsColumnName entity=baseService.baseSelectByPrimaryKey(id);
        return entity;
    }
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑自定义列名信息",response = JSONObject.class, notes = "编辑自定义列名信息")
    public @ResponseBody JSONObject editWmsColumnName(@RequestBody WmsColumnName entity) {
        try{
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"编辑自定义列名信息成功","", JSONObject.fromObject(entity).toString());
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "编辑自定义列名信息失败", e.getMessage(), "");
        }
    }
}
